<?php
if ($_REQUEST['vote']) {
	$weeknr=date('Y').date('W');
	$cookiename = 'photovoted'.$imgid;
	if ($_COOKIE[$cookiename] == $weeknr) {
		$SITE_MIDDLE .= FormatElement( GetLangString ( $lang, "txt_vote"), GetLangString ( $lang, "msg_alreadyvoted")); 
	} else {
		setcookie($cookiename,$weeknr);
		$imgdata['votes']++;
		if ($_REQUEST['vote']=="+") {
			$query = "UPDATE photos set votes=votes+1 where id = $imgid";
			$result = mysql_query($query);
			$query = "UPDATE photos set score=score+1 where id = $imgid";
			$result = mysql_query($query);
			$imgdata['score']++;
		} else {
			$query = "UPDATE photos set votes=votes+1 where id = $imgid";
			$result = mysql_query($query);
			$query = "UPDATE photos set score=score-1 where id = $imgid";
			$result = mysql_query($query);
			$imgdata['score']--;
		}
		$SITE_MIDDLE .= FormatElement( GetLangString ( $lang, "txt_vote"), GetLangString ( $lang, "msg_voted")); 
	} 
}

	$vote			.= "<div class=\"vote\"><center><form method=\"POST\" ACTION=\"/index.php?do=viewphoto\">"; 
	$vote 		.= "<input value=\"$imgid\" type=\"hidden\" name=\"photo_id\">";
	$vote 		.= "<input value=\"-\" name = \"vote\" type=\"submit\"><input value=\"+\" name = \"vote\" type=\"submit\">";
	$vote .= "</form></center></div>"  ;
/*	
*/
if ($_REQUEST['fav']=='add'){
	$query = "insert into `photofavs` (`user`,`photo`) values ('$id','$imgid')";
	$result = mysql_query($query);
	if ($result) {
		$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_favs"),GetLangString($lang,"msg_favadded"));
	} else {	
		$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_error"),GetLangString($lang,"msg_dberror"));
	}		
				
} else
if ($_REQUEST['fav']=='delete'){
	$query = "delete from `photofavs` where `user`='$id' and `photo`='$imgid'";
	$result = mysql_query($query);
	if ($result) {
		$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_favs"),GetLangString($lang,"msg_favdeleted"));
	} else {	
		$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_error"),GetLangString($lang,"msg_dberror"));
	}		
} 															
															
//-- adding favourites (like hyves.nl respect)
$query = "select * from `photofavs` where `photo`='$imgid'";
$result = mysql_query($query);
while ($fav = mysql_fetch_array($result)){
	if ($fav['user']==$id) $myfav = true;
	$query2= "select * from `users` where `id`='". $fav['user']."'";
	$result2 = mysql_query($query2);
	$favinfo = mysql_fetch_array($result2);
	$favs .= " <a href='/index.php?do=profile&profile_id=".$favinfo['id']."'>							
			<div class=\"usersmallthumb\">
			<img src='index.php?do=getphoto&size=50&&square=1&id=".$favinfo['photo']."' \" title=\"$TXT_NOIMG\" 
				width=\"50\" height=\"50\">".$favinfo['name']."</div></a>";
}

if ($myfav) {
	$favs .= "<br><a href='index.php?do=viewphoto&photo_id=$imgid&fav=delete'>".
			GetLangString($lang,"txt_delfav") ."</a>";
} else {
	$favs .= "<br><a href='index.php?do=viewphoto&photo_id=$imgid&fav=add'>".
			GetLangString($lang,"txt_addfav")."</a>";
}
	if (!$RENDER_MOBILE) $SITE_MIDDLE .= "<table width='100%'><tr><td width='50%' valign='top'>";
	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_favs"),$favs);
	if (!$RENDER_MOBILE) $SITE_MIDDLE .= "</td><td width='50%' valign='top'>";
	$SITE_MIDDLE .= FormatElement( GetLangString ($lang, "txt_vote") , 
															str_ireplace("%votes%",$imgdata['votes'], 
															str_ireplace("%score%",$imgdata['score'],
															GetLangString ($lang, "msg_photovote"))).$vote); 
	if (!$RENDER_MOBILE) $SITE_MIDDLE .="</td></tr></table>";


//--
$query	=	"SELECT * from photocomments where `to`=$imgid and (type=1 or type=0)";

$result = mysql_query($query);
if (mysql_num_rows($result)){ 
	while ($comment = mysql_fetch_array($result)) {
	 
		$query        = "SELECT * from users where id='".$comment['from']."'";
		$result_name	= mysql_query($query);
		if (mysql_num_rows($result_name)){ 
/*
			$from = mysql_result ($result_name,0);
			//$f_name = mysql_result ($result_name,0);
			$from = "<a href=\"index.php?do=profile&profile_name=$from\">$from</a>";
*/
			$profile_info=mysql_fetch_array($result_name);
			
			
			$from = "<a href=\"index.php?do=profile&profile_id=".$profile_info['id']."\">".$profile_info['name']."</a><br>";
			$from .= GetStatusImg($profile_info['status']);					
			$from .= "<br><img src=\"index.php?do=getphoto&square=1&size=75&id=".$profile_info['photo']."\" title=\"".$profile_info['name']."\">";
			
			$reply  = "<a href='index.php?do=sendpm&to=".$comment['from']."'>".GetLangString($lang,"txt_reply")."</a> - ";

			
		} else $from .= $TXT_UNKNOWN;
		
		$time = date("d-m-Y H:i:s",$comment['time']);
		$text = bb2html($comment['text']);
		
		if ( ($id==$comment['to']) or ($id==$comment['from']) ) {
			$delmsg = "<a href=\"index.php?do=pm&photodelete=".$comment['id']."\">".GetLangString($lang,"txt_delete")."</a>"; 
		} else {
			$delmsg="";
		}


		$COMMENTS = "<tr>
			<td align=\"center\" width=\"125\" valign=\"top\">
			$from<br>$time<br>$reply  $delmsg  
			</td>	<td valign=\"top\">$text</td></tr>$COMMENTS";
	}

	$COMMENTS = bb2html("<table border=\"1\" width=\"100%\">$COMMENTS</table>");
	$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_comments"),$COMMENTS);
	
	
} else $SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_comments"),GetLangString($lang,"txt_nocomments"));

//$COMMENTS  = '<div class=label>Send a message</div>';
$COMMENTS = '<form method="POST" ACTION="index.php?do=pm">';
$COMMENTS .= '<textarea name="text" rows="5"></textarea>';
$COMMENTS .= "<input type=\"hidden\" name=\"from\" value=\"$id\">";
$COMMENTS .= "<input type=\"hidden\" name=\"to\" value=\"$imgid\">";

/*$COMMENTS .= "Message Type:<select name=\"type\" size=\"2\">
	<option selected value=\"0\">".GetLangString($lang,"txt_comments")."</option>
	<option  value=\"2\">".GetLangString($lang,"txt_pm")."</option>
</select>";
*/
$SITE_MIDDLE .= "<input type=\"hidden\" name=\"type\" value=\"0\">";
$COMMENTS .= "<input value=\"".GetLangString($lang,"txt_send")."\"	name = \"photoadd\" 	type=\"submit\">" ;
$COMMENTS .= '</form>';
//$SITE_MIDDLE .=	"</div>"
$SITE_MIDDLE .= FormatElement(GetLangString($lang,"txt_send"),$COMMENTS);

			

?>
